package isg3;

import data.*;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

public class GestiondeEventos {
	
	 private static GestiondeEventos ge;
	 private JDBCEventoDAO evenDAO= new JDBCEventoDAO();
	    private List eventos;
	    private List usuarios;
	    public static synchronized GestiondeEventos getInstance() {
	        if (ge == null)
	            ge = new GestiondeEventos();
	        return ge;
	    }

	    private GestiondeEventos() {
	        eventos = (new JDBCEventoDAO()).selectAllEventos();
	        usuarios= (new JDBCPersonaDAO()).selectAllPersonas();
	    }
	    
	   

	    public List getEventos() {
	        return eventos;
	       
	    }
	    public List getPersonas(){
	    	return usuarios;
	    }

	    public Evento getEvento(String IDevento) {
	        Evento result = null;
	        for (Iterator iter = eventos.iterator(); iter.hasNext();) {
	            Evento ev = (Evento) iter.next();
	            if (ev.getIDEvento().compareTo(IDevento) == 0) {
	                result = ev;
	            }
	        }
	        return result;
	    }
	    public Persona getPersona(String IDpersona) {
	        Persona result = null;
	        for (Iterator iter = eventos.iterator(); iter.hasNext();) {
	           Persona p = (Persona) iter.next();
	            if (p.getNIF().compareTo(IDpersona) == 0) {
	                result = p;
	            }
	        }
	        return result;
	    }
	    public String getEventoOID(String IDEvento){
	    	String oid=null;
	    	if (IDEvento==null){
	    		throw new NullPointerException("Error, no puede ser vacio");
	    	}else{
	    		oid=evenDAO.selecEventoOID(IDEvento);
	    	}
	    	
	    	return oid;
	    }

	public void anadeVoto(Persona u, Evento v, Calendar d){
		v.getListaCorreo().add(u.getCorreo());
		Calendar dia = v.getFechaInicio();
		int i = 0;
		while(dia.compareTo(d) < 0){
			dia.add(Calendar.DAY_OF_MONTH, 1);
			i++;
		}
		v.getArraySemanaVoto()[i]++;
	}
	
	public List crearListaCorreoEventos(Evento v){
		return v.getListaCorreo();
	}
	
	public void getGanadorVotacion(Evento v){
		Calendar d = v.getFechaInicio();
		int pos = 0;
		int[] array = v.getArraySemanaVoto();
		int max = array[0];
		for(int i =1;i<v.getArraySemanaVoto().length;i++){
			if(array[i] > max){
				max = array[i];
				pos = i;
			}
		}
		
			d.add(Calendar.DAY_OF_MONTH, pos);
		    v.setFechaGanada(d);
	}
}
